﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BestDocking.Docking;

namespace QuanLyKhachHang
{
    public partial class fNhanVien :  DockContent
    {
        string NguoiDungHienTai = "NV00002";
        public fNhanVien()
        {
            InitializeComponent();
        }
        public fNhanVien(string NguoiDungHienTai)
        {
            InitializeComponent();
            this.NguoiDungHienTai = NguoiDungHienTai;
        }
        void LoadComBo()
        {
            cbNhom.DataSource = Data.NVNhomNhanVien.ChonTatCa();
            cbNhom.DisplayMember = "TenNhomNhanVien";
            cbNhom.ValueMember = "MaNhomNhanVien";
        }

        bool KiemTraThongTinNhap()
        {
            if (string.IsNullOrEmpty(txtMaNhanVien.Text.ToString().Trim()) || (string.IsNullOrEmpty(txtTenNhanVien.Text.ToString().Trim())) || (string.IsNullOrEmpty(txtDiaChi.Text.ToString().Trim())) || (string.IsNullOrEmpty(txtDienThoai.Text.ToString().Trim())))
            {
                MessageBox.Show("Bạn vui lòng kiểm tra lại thông tin nhập.","Loi nhap du lieu",MessageBoxButtons.OK,MessageBoxIcon.Error);
                return false;
            }
            else
            {
                return true;
            }
        }
        void DieuKhien()
        {
            btnSua.Enabled = btnXoa.Enabled = btnXuat.Enabled = (dgvDanhSach.RowCount != 0);
        }
        BindingSource bds = new BindingSource();
        void LoadDuLieu()
        {
            LoadComBo();
            bds.DataSource = Data.NVNhanVien.ChonTatCa();
            dgvDanhSach.DataSource = bds;
            DieuKhien();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (KiemTraThongTinNhap()==false)
            {
                return;
            }
            if (cbNhom.SelectedIndex == -1)
            {
                MessageBox.Show("Bạn chưa chọn nhóm nhân viên","Loi kieu du lieu");
                return;
            }
            Data.TTNhanVien NhanVien = new Data.TTNhanVien();
            NhanVien.MaNhanVien = SinhMa.Sinh("NV",Data.NVNhanVien.ChonTatCa(),"00000");
            NhanVien.TenNhanVien = txtTenNhanVien.Text.Trim();
            NhanVien.DiaChi = txtDiaChi.Text.Trim();
            NhanVien.DienThoai = txtDienThoai.Text.Trim();
            NhanVien.Email = txtEmail.Text.Trim();
            NhanVien.MaNhomNhanVien = cbNhom.SelectedValue.ToString();
            Data.NVNhanVien.ThemNhanVien(NhanVien);
            LoadDuLieu();
        }

        private void fNhanVien_Load(object sender, EventArgs e)
        {
            LoadDuLieu();
            txtMaNhanVien.DataBindings.Clear();
            txtMaNhanVien.DataBindings.Add("Text", bds, "MaNhanVien", true, DataSourceUpdateMode.Never);
            txtTenNhanVien.DataBindings.Clear();
            txtTenNhanVien.DataBindings.Add("Text", bds, "TenNhanVien", true, DataSourceUpdateMode.Never);
            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", bds, "DiaChi", true, DataSourceUpdateMode.Never);    
            txtDienThoai.DataBindings.Clear();
            txtDienThoai.DataBindings.Add("Text", bds, "DienThoai", true, DataSourceUpdateMode.Never);    
            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", bds, "Email", true, DataSourceUpdateMode.Never);
            cbNhom.DataBindings.Clear();
            cbNhom.DataBindings.Add("SelectedValue", bds, "MaNhomNhanVien", true, DataSourceUpdateMode.Never);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (KiemTraThongTinNhap() == false)
            {
                return;
            }
            Data.TTNhanVien NhanVien = new Data.TTNhanVien();
            NhanVien.MaNhanVien = txtMaNhanVien.Text.Trim();
            NhanVien.TenNhanVien = txtTenNhanVien.Text.Trim();
            NhanVien.DiaChi = txtDiaChi.Text.Trim();
            NhanVien.DienThoai = txtDienThoai.Text.Trim();
            NhanVien.Email = txtEmail.Text.Trim();
            NhanVien.MaNhomNhanVien = cbNhom.SelectedValue.ToString();
            try
            {
                Data.NVNhanVien.SuaNhanVien(NhanVien);
            }
            catch (Exception)
            {
                MessageBox.Show("Mã nhân viên vừa nhập vào không có.", "Loi du lieu", MessageBoxButtons.OK, MessageBoxIcon.Error);
              
            }
            LoadDuLieu();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (KiemTraThongTinNhap()==false)
            {
                return;
            }
            try
            {
                Data.NVNhanVien.XoaNhanVien(txtMaNhanVien.Text.Trim());
            }
            catch (Exception)
            {
                MessageBox.Show("Mã nhân viên vừa nhập vào không có.", "Loi du lieu", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
            LoadDuLieu();
        }

        private void dgvDanhSach_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dgvDanhSach_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                    if ((string.IsNullOrEmpty(dgvDanhSach["dgvMaNhanVien", e.RowIndex].Value.ToString()) == false) && (dgvDanhSach["dgvMaNhanVien", e.RowIndex].Value.ToString() == NguoiDungHienTai) && (dgvDanhSach["dgvMaNhanVien", e.RowIndex].Value.ToString() != "NV00001"))
                    {
                        fTaiKhoan f = new fTaiKhoan(dgvDanhSach["dgvMaNhanVien", e.RowIndex].Value.ToString(), dgvDanhSach["dgvUserName", e.RowIndex].Value.ToString(), dgvDanhSach["dgvPassWord", e.RowIndex].Value.ToString());
                        f.ShowDialog();
                         LoadDuLieu();
                        return;
                    }
                   if(NguoiDungHienTai=="NV00001")
                    { 
                        fTaiKhoan f = new fTaiKhoan(dgvDanhSach["dgvMaNhanVien", e.RowIndex].Value.ToString(), dgvDanhSach["dgvUserName", e.RowIndex].Value.ToString(), dgvDanhSach["dgvPassWord", e.RowIndex].Value.ToString());
                        f.ShowDialog();
                        LoadDuLieu();
                        return;
                    }
            }
            catch (Exception)
            {
                return;
            }
          
            
        }

        private void cbNhom_MouseHover(object sender, EventArgs e)
        {
            if (Data.NVNhomNhanVien.ChonTatCa().Rows.Count != cbNhom.Items.Count)
            {
                LoadComBo();
            }
        }


    }
}
